/**
 * @param {number[]} nums
 * @return {number}
 */
var countHillValley = function (nums) {
    let res = 0;
    let preDiff = nums[1] - nums[0];
    for (let i = 2; i < nums.length; i++) {
        const diff = nums[i] - nums[i - 1];
        if ((preDiff < 0 && diff > 0) || (preDiff > 0 && diff < 0)) {
            res++;
            preDiff = diff;
        }
        if (preDiff == 0) {
            preDiff = diff;
        }
    }
    return res;
};
console.log(countHillValley([2, 4, 1, 1, 6, 5]));
console.log(countHillValley([6, 6, 5, 5, 4, 1]));
